# -*- coding: utf-8 -*-

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import HttpResponseNotFound

from django.template import RequestContext
from django.template.loader import get_template

from django.contrib.auth import get_user
from django.contrib.auth.decorators import login_required

from people.handle import _get_avatar_id

from forms import *
from models import *

from handle import _get_gridfs_ids

#@login_required
def check(request, uid):
    """
    Serve people's request for specific place
    """
    username = avatar_id = None
    place = Place.objects.get(uid=uid)
    if request.user.is_authenticated():
        username = request.user.username
        avatar_id = _get_avatar_id(username)
    title = place.name;
    latitude = place.location[0]
    longitude = place.location[1]
    if len(place.subjects) > 0:
        subjects = _get_gridfs_ids(place.subjects)
    else:
        subjects = None
    print(subjects)
    place_comments = place.comments
    template = get_template('place/check.dtpl')
    place_comment_form = PlaceCommentForm()
    print(place_comment_form)
    context = RequestContext(request, { 'title': title
                                        ,'uid': uid
                                        ,'username': username
                                        ,'avatar_id': avatar_id
                                        ,'subjects': subjects
                                        ,'place': place
                                        ,'latitude': latitude
                                        ,'longitude': longitude
                                        ,'place_comments': place_comments
                                        ,'place_comment_form': place_comment_form
                                        })

    response = HttpResponse(template.render(context))

    return response


def subject(request, uid, index):
    """
    Serve people's request for specific subject of place
    """
    username = avatar_id = None
    next_subject_index = prev_subject_index = None
    index = int(index)
    print(uid)
    place = Place.objects.get(uid=uid)
    if request.user.is_authenticated():
        username = request.user.username
        avatar_id = _get_avatar_id(username)
    name = place.name
    title = place.country + '-' + place.city + '-' + place.name
    #latitude = place.location[0]
    #longitude = place.location[1]
    subject = place.subjects[index]
    image_id = str(subject.image.grid_id)
    #print(photo_id)
    #place_comments = place.comments
    template = get_template('place/subject.dtpl')
    #place_comment_form = PlaceCommentForm()
    #print(place_comment_form)
    if index == 0:
        if len(place.subjects) > 1:
            next_subject_index = str(index + 1)
    elif index == (len(place.subjects) - 1):
        if index > 0:
            prev_subject_index = str(index - 1)
    else:
        next_subject_index = str(index + 1)
        prev_subject_index = str(index - 1)
    print(index)
    #print(prev_photo_index)
    context = RequestContext(request, { 'title': title
                                        ,'name': name
                                        ,'uid': uid
                                        ,'username': username
                                        ,'avatar_id': avatar_id
                                        ,'place': place
                                        ,'subject': subject
                                        ,'image_id': image_id
                                        ,'next_subject_index': next_subject_index
                                        ,'prev_subject_index': prev_subject_index
                                        #,'latitude': latitude
                                        #,'longitude': longitude
                                        #,'place_comments': place_comments
                                        #,'place_comment_form': place_comment_form
                                        })

    response = HttpResponse(template.render(context))

    return response                 
